// 账号相关接口
import request from "../request";
import type { getRoleInfoItem, IMenu, IAdminListItem, IAdminListParams, IAdminStatRes, IAdminListRes } from "@/types/global";
import qs from "qs";
// 接口参数类型
interface IAdminLoginParams {
  username: string;
  password: string;
}
// 接口返回值类型
interface IAdminLoginRes {
  token: string;
  tokenHead: string;
}

interface ILoginInfo {
  roles: string[];
  icon?: any;
  menus: IMenu[];
  username: string;
}

// 切换开关状态
interface updateStatusParams {
	id: number;
	status: number;
}

//
interface AssignRolesParams {
	adminId: number;
	roleIds: number[];
}
// api
// 用户登录获取token
const LoginAPI = (params: IAdminLoginParams): Promise<IAdminLoginRes> =>
  request.post("/admin/login", params);
// 获取用户信息
const LoginInfoAPI = (): Promise<ILoginInfo> => request.get("/admin/info");
// 获取用户列表
const AdminListAPI = (data?: IAdminListParams): Promise<IAdminListRes> =>
  request.get("/admin/list", { params: data });
// 更新用户数据
const adminUpdateAPI = (data: IAdminListItem): Promise<number> =>
  request.post(`/admin/update/${data.id}`, data);
// 注册用户updateStatus
const adminRegisterAPI = (data?: IAdminListItem): Promise<IAdminListRes> =>
  request.post("/admin/register", data);
// 首页图表信息
const getAdminStatAPI = (): Promise<IAdminStatRes> => request.get("http://kumanxuan1.f3322.net:8360/admin/stat");
// 修改用户状态
const updateStatusAPI = (data: updateStatusParams) => request.post(`/admin/updateStatus/${data.id}?status=${data.status}`);
// 获取指定用户信息
const getRoleInfoAPI = (id: number): Promise<getRoleInfoItem[]> => request.get(`/admin/role/${id}`);
// 分配角色
const AssignRolesAPI = (params: AssignRolesParams) =>
	request.post("/admin/role/update", qs.stringify(params, { arrayFormat: "comma", encode: false }));

export { AssignRolesAPI, LoginAPI, LoginInfoAPI, AdminListAPI, adminUpdateAPI, adminRegisterAPI, getAdminStatAPI, updateStatusAPI, getRoleInfoAPI };
